<head>
  <style> body { margin: 0; } </style>

  <script type="importmap">{ "imports": {
    "react": "https://esm.sh/react",
    "react-dom": "https://esm.sh/react-dom/client"
  }}</script>

<!--  <script type="module">import * as React from 'react'; window.React = React;</script>-->
<!--  <script src="../../dist/react-globe.gl.js" defer></script>-->
</head>

<body>
<div id="globeViz"></div>

<script src="//unpkg.com/@babel/standalone"></script>
<script type="text/jsx" data-type="module">
  import Globe from 'https://esm.sh/react-globe.gl?external=react';
  import React, { useState, useEffect } from 'react';
  import { createRoot } from 'react-dom';
  import { scaleLinear } from 'https://esm.sh/d3-scale';

  const World = () => {
    const [equakes, setEquakes] = useState([]);

    useEffect(() => {
      // load data
      fetch('//earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_month.geojson').then(res => res.json())
        .then(({ features }) => setEquakes(features));
    }, []);

    const weightColor = scaleLinear()
      .domain([0, 30])
      .range(['lightblue', 'darkred'])
      .clamp(true);

    return <Globe
      globeImageUrl="//cdn.jsdelivr.net/npm/three-globe/example/img/earth-night.jpg"

      hexBinPointsData={equakes}
      hexBinPointLat={d => d.geometry.coordinates[1]}
      hexBinPointLng={d => d.geometry.coordinates[0]}
      hexBinPointWeight={d => d.properties.mag}
      hexAltitude={({ sumWeight }) => sumWeight * 0.0025}
      hexTopColor={d => weightColor(d.sumWeight)}
      hexSideColor={d => weightColor(d.sumWeight)}
      hexLabel={d => <div>
        <b>{d.points.length}</b> earthquakes in the past month:
        <ul>
          {d.points.slice()
            .sort((a, b) => b.properties.mag - a.properties.mag)
            .map(d => <li>{d.properties.title}</li>)
          }
        </ul>
      </div>}
    />;
  };

  createRoot(document.getElementById('globeViz'))
    .render(<World />);
</script>
</body>